import _ from 'lodash'
import { section } from './lablog.js'
import {IO, map, Maybe} from './util.js'

const $ = function(selector) {
    return new IO(function() {
        return selector.name
    })
}

const getVal = _.flowRight(map(_.property('value')), $)


const signIn = _.curry(
    // function(username, password, remember) {
    function(username) {
        const password = "abcdefg", remember = true
        const loginResult = `${username} use ${password.length} signed! [${!remember ? '×' : '√'}]`
        // console.log()
        return loginResult
    }
)

const selectorName = {
    name: {value: "HHG"},
}
const selectorPass = {
    name: {value: "123456"}
}
const selectorRemember = {
    name: {value: false}
}
/**
 * IO.of(FsignIn).ap(getVal(selectorName))
 * = IO.of(FsignIn).ap(IO(() => name.v))
 * = IO(() => FsignIn).map(signIn)
 * = IO(_.flowRight(() => FsignIn, () => name.v))
 * 
 * new IO(signIn).ap(getVal(selectorName))
 * = IO(() => name.v).map(signIn)
 * = IO.of(__flowRight(signIn, () => name.v))
 * 
 * IO(() => __flowRight(signIn, () => name.v)).ap(getVal(selectorPass))
 * = IO.of(IO(() => pass.v))
 */

// const v = new IO(signIn).ap(getVal(selectorName)).ap(getVal(selectorPass)).ap(getVal(selectorRemember))
const v = new IO(signIn).ap(getVal(selectorName))
console.log(v.__value())
// const step1 = IO.of(signIn).ap(getVal(selectorName))
// console.log("step1", step1.__)
// console.log("step1.__value()('ch', true) =", step1.__value()('ch', true))
// const step2 = step1.ap(getVal(selectorPass))
// console.log("step2.__value()(false) =", step2.__value()(false))
// const step3 = step2.ap(getVal(selectorRemember))
// console.log("step3:", step3.__value())


const signOut = _.curry(function(name, result) {
    return `${name} ${result} signout.`
})

// Container(Value)
const valueSection = section("use for Container(value)")
valueSection(() => {
    const mapV = Maybe.of(signOut).ap(Maybe.of(selectorName.name.value)).ap(Maybe.of(selectorPass.name.value))
    console.log(mapV)
})


const partEffect = section("test effect partly")
partEffect(() => {
    const hello = new IO((name) => {return `hello, ${name}`})
    const ioV = hello.ap(getVal(selectorName))
    console.log(ioV.__value())
})